package jjn.round2;

/**
 * @author Jiang Jining
 * @since 2023-06-03 20:48
 */
public class LeetCode42_TrappingRainWater {
    public int trap(int[] height) {
        int sum = 0;
        //找到最大的高度，以便遍历。
        int max = getMax(height);
        for (int i = 1; i <= max; i++) {
            //标记是否开始更新 temp
            boolean isStart = false;
            int tempSum = 0;
            for (int k : height) {
                if (isStart && k < i) {
                    tempSum++;
                }
                if (k >= i) {
                    sum = sum + tempSum;
                    tempSum = 0;
                    isStart = true;
                }
            }
        }
        return sum;
    }
    
    private int getMax(int[] height) {
        int max = 0;
        for (int j : height) {
            if (j > max) {
                max = j;
            }
        }
        return max;
    }
}
